import { _decorator, Component, Node, Sprite, Animation, AnimationClip } from 'cc';
import { AppControll, ConfigKey, PopView } from '../manager/AppControll';
import { PopManager } from '../manager/PopManager';
import { SoundManager } from '../manager/SoundManger';
import { GameData } from '../system/GameData';
import { DataHelper } from '../system/DataHelper';
import { SystemMessage } from '../pop/SystemMessage';
const { ccclass, property } = _decorator;

@ccclass('DailyWelfareBtn')
export class DailyWelfareBtn extends Component {
    @property(Node)
    mNdClose: Node = null;

    private interval = 20;
    private cur = 0;
    private mNdPoint: Node = null;
    private mNdShopPoint: Node = null;
    private mNdTrunTablePoint: Node = null;
    private mNdSignPoint: Node = null;
    private mBIsLock: boolean = true;
    private mNdLock: Node = null;
    private mNdWelfareList: Node = null;
    private mIsOpen: boolean = false;
    protected onLoad(): void {
        this.mNdPoint = this.node.getChildByName("yellowPoint");
        this.mNdLock = this.node.getChildByName("unlockText");
        this.mNdWelfareList = this.node.getChildByName("welfareList");
        this.mNdShopPoint = this.mNdWelfareList.getChildByName("shop").getChildByName("shopBtn").getChildByName("yellowPoint");
        this.mNdTrunTablePoint = this.mNdWelfareList.getChildByName("turntable").getChildByName("turntableBtn").getChildByName("yellowPoint");
        this.mNdSignPoint = this.mNdWelfareList.getChildByName("signIn").getChildByName("signInBtn").getChildByName("yellowPoint");
        this.node.on(Node.EventType.TOUCH_END, this.OnTouch, this);
        this.mBIsLock = false;
        this.mBIsLock = GameData.playerData.Honor < Number.parseInt(DataHelper.GetConfigByKey(ConfigKey.sevenSign).value);
        if (this.mBIsLock == false) {
            this.mNdLock.active = false;
            this.node.getComponent(Sprite).grayscale = false;
        } else {
            this.mNdLock.active = true;
            this.node.getComponent(Sprite).grayscale = true;
        }
        this.mNdClose.on(Node.EventType.TOUCH_END, this.OnClose, this);
        this.mNdClose.active = false;
    }

    OnClose() {
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        this.mIsOpen = false
        let state = this.mNdWelfareList.getComponent(Animation).getState("fuli");
        state.wrapMode = AnimationClip.WrapMode.Reverse;
        if (!state.isPlaying) {
            this.mNdWelfareList.getComponent(Animation).play();
        }
    }

    OnTouch() {
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        if (this.mBIsLock) {
            PopManager.Instance<PopManager>().ShowPop(PopView.systemMessage, (node) => {
                node.getComponent(SystemMessage).InitContent("尚未达到解锁条件")
            })
            return;
        }
        let state = this.mNdWelfareList.getComponent(Animation).getState("fuli");
        if (!this.mIsOpen) {
            this.mIsOpen = true
            this.mNdClose.active = true;
            state.wrapMode = AnimationClip.WrapMode.Normal;
            this.mNdWelfareList.getComponent(Animation).play();
        } else {
            this.mIsOpen = false
            this.mNdClose.active = false;
            state.wrapMode = AnimationClip.WrapMode.Reverse;
            this.mNdWelfareList.getComponent(Animation).play();
        }

    }


    protected update(dt: number): void {
        this.cur++;
        if (this.cur % this.interval == 0) {
            GameData.GetCurSignIn();
            this.mBIsLock = GameData.playerData.Honor < Number.parseInt(DataHelper.GetConfigByKey(ConfigKey.sevenSign).value);
            let freeTimes = GameData.GetTurnFreeTimes();
            let freeShop = GameData.playerData.secretShopFreeTimes
            if (this.mBIsLock == false) {
                this.mNdLock.active = false;
                this.node.getComponent(Sprite).grayscale = false;
                if (GameData.playerData.Honor - Number.parseInt(DataHelper.GetConfigByKey(ConfigKey.sevenSign).value) <= 60 && GameData.playerData.singnGuide) {
                    GameData.playerData.singnGuide = false;
                    PopManager.Instance<PopManager>().ShowPop(PopView.sevenSignIn);
                }
            } else {
                this.mNdLock.active = true;
                this.node.getComponent(Sprite).grayscale = true;
            }
            if ((GameData.playerData.signInCount < 1 || freeTimes > 0 || freeShop > 0) && this.mBIsLock == false) {
                this.mNdPoint.active = true;
            } else {
                this.mNdPoint.active = false;
            }
            if (GameData.playerData.signInCount < 1 && this.mBIsLock == false) {
                this.mNdSignPoint.active = true;
            } else {
                this.mNdSignPoint.active = false;
            }
            if (freeShop > 0) {
                this.mNdShopPoint.active = true;
            } else {
                this.mNdShopPoint.active = false;
            }
            if (freeTimes > 0) {
                this.mNdTrunTablePoint.active = true;
            } else {
                this.mNdTrunTablePoint.active = false;
            }
        }
    }

    OnBtnOpenSign() {
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        PopManager.Instance<PopManager>().ShowPop(PopView.sevenSignIn);
    }

    OnBtnOpenTurntable() {
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        PopManager.Instance<PopManager>().ShowPop(PopView.dailyTurntable);
    }

    OnBtnOpenSecretShop() {
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        PopManager.Instance<PopManager>().ShowPop(PopView.secretShop);
    }
}


